package com.client.common;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.io.File;

/**
 * Created by Administrator on 2016/12/18.
 */
public class Sound {
	String path = new String("sound\\");
	String file = new String("notify.mid");

	Sequence sequence;//序列

	Sequencer midi;//定义一个Sequencer类型的变量
	
	boolean sign;//标志，true为正在播放，反之没有播放
	
	public Sound() {
		loadSound();
	}

	private void loadSound() { //播放音乐
		try {
			sequence = MidiSystem.getSequence(new File(path + file));//读取音乐文件
			midi = MidiSystem.getSequencer();//创建一个音乐设备播放器
			midi.open();//打开音乐设备播放器
			midi.setSequence(sequence);//把文件传给设备播放器
			midi.start();//开始播放
			midi.setLoopCount(1);//播放次数
		} catch (Exception e) {
			e.printStackTrace();
		}
		sign = true;
	}

	public void SoundStop() {
		if (isPlay()) {
			midi.stop();
			midi.close();
			sign = false;
		}
	}

	public boolean isPlay () {
		return sign;
	}

	public static void main(String[] args) {
		new Sound();
	}
}
